///@file TestMatrix.cpp
///@brief Implementation of unit tests for Matrix object
///@author Arnaud Duval
///@version 0.0
///@date 2012/03/05

#include <iostream>
#include <cmath>
#include <string>
#include <sstream>

#include <Matrix.h>

///@brief Test of Vector members
int main(int argc, char** argv)
{
	/*if(argc == 1)
		return 0;

	std::string s(argv[1]);
	std::istringstream stream(s);
	unsigned int dim;

	stream >> dim;
	if(stream.fail())
		return 1;*/

	unsigned int dim = 4;


	// Test : creation of a random symmetric matrix
	Matrix testMat(dim, 'r');

	// Test : verify if matrix is symmetric
	if(!testMat.IsSymmetric(1.e-8))
		return 1;

	// Test : inversion of symmetric matrix with Gauss-Jordan method
	Matrix invTestMat = testMat.InvertGaussJordan();
	Matrix testUnit = testMat|invTestMat;

	for(unsigned int i = 1 ; i <= dim ; ++i)
	{
		for(unsigned int j = 1 ; j <= dim ; ++j)
		{
			if(i == j)
			{
				if(fabs(testUnit(i,j)-1.) > 1.e-10)
					return 1;
			}
			else
			{
				if(fabs(testUnit(i,j)) > 1.e-10)
					return 1;
			}
		}
	}

	// Test : inversion of symmetric matrix with Cholesky method

	Matrix invTestMatC = testMat.InvertCholesky();
	Matrix testUnitC = testMat|invTestMatC;

	for(unsigned int i = 1 ; i <= dim ; ++i)
	{
		for(unsigned int j = 1 ; j <= dim ; ++j)
		{
			if(i == j)
			{
				if(fabs(testUnitC(i,j)-1.) > 1.e-10)
					return 1;
			}
			else
			{
				if(fabs(testUnitC(i,j)) > 1.e-10)
					return 1;
			}
		}
	}

	// Test : inversion of 2x2 matrix with analytic method
	Matrix mat2(2, 'R');
	Matrix inv2 = mat2.InvertAnalytic();
	Matrix test2 = mat2 | inv2;

	for(unsigned int i = 1 ; i <= 2 ; ++i)
	{
		for(unsigned int j = 1 ; j <= 2 ; ++j)
		{
			if(i == j)
			{
				if(fabs(test2(i,j)-1.) > 1.e-10)
					return 1;
			}
			else
			{
				if(fabs(test2(i,j)) > 1.e-10)
					return 1;
			}
		}
	}

	// Test : inversion of 3x3 matrix with analytic method
	Matrix mat3(3, 'R');
	Matrix inv3 = mat3.InvertAnalytic();
	Matrix test3 = mat3 | inv3;

	for(unsigned int i = 1 ; i <= 3 ; ++i)
	{
		for(unsigned int j = 1 ; j <= 3 ; ++j)
		{
			if(i == j)
			{
				if(fabs(test3(i,j)-1.) > 1.e-10)
					return 1;
			}
			else
			{
				if(fabs(test3(i,j)) > 1.e-10)
					return 1;
			}
		}
	}



    
    return 0;
}

